package com.mebs.model.logistics;

import com.mebs.model.inventory.OrderItem;
import com.mebs.model.inventory.BoxSubItem;

import javax.persistence.*;

import org.jt.jet.annotation.Field;
import org.jt.jet.annotation.ArgumentType;
import static org.hibernate.proxy.HibernateProxyHelper.getClassWithoutInitializingProxy;

/**
 * Created by Sergey Prasolov, sap@jensentechnologies.com Date: Mar 10, 2006, 11:23:55 AM
 */
@Entity()
@Table(name = "STRUCTURED_BOX_ITEM")
public class BoxItem {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "Id")
    private Long id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "BoxId")
    private StructuredBox box;

    @ManyToOne(optional = false)
    @JoinColumn(name = "OrderItemId")
    private OrderItem orderItem;

    @ManyToOne(optional = true)
    @JoinColumn(name = "SubItemId")
    @Field(type = ArgumentType.INSTANCE, optional = true)
    private BoxSubItem subItem;

    @Column(name = "Quantity", nullable = true)
    private Integer quantity;

    /**
     * ********************** Getters-setters ***********************
     */
    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public StructuredBox getBox()
    {
        return box;
    }

    public void setBox(StructuredBox box)
    {
        this.box = box;
    }

    public OrderItem getOrderItem()
    {
        return orderItem;
    }

    public void setOrderItem(OrderItem orderItem)
    {
        this.orderItem = orderItem;
    }

    public Integer getQuantity()
    {
        return quantity;
    }

    public void setQuantity(Integer quantity)
    {
        this.quantity = quantity;
    }

    public BoxSubItem getSubItem()
    {
        return subItem;
    }

    public void setSubItem(BoxSubItem subItem)
    {
        this.subItem = subItem;
    }

    @Transient
    public boolean isDeletable()
    {
        return true;
    }

    @Override
    public String toString()
    {
        return ((this.orderItem == null) ? "" : this.orderItem.toString())
            + ((this.subItem == null) ? "" : "/" + this.subItem.toString());
    }

    @Override
    public int hashCode()
    {
        if (id == null) return super.hashCode();
        return id.hashCode();
    }

    @Override
    public boolean equals(Object o)
    {
        if (o == null) return false;
        if (this == o) return true;
        if (id == null) return false;
        if (getClassWithoutInitializingProxy(this) != getClassWithoutInitializingProxy(o))
            return false;
        final BoxItem that = (BoxItem) o;
        return id.equals(that.getId());
    }

}
